﻿using Ags.MonsterTools.Common;
using Microsoft.Practices.Prism.Logging;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.UnityExtensions;
using Microsoft.Practices.Unity;
using System.IO;
using System.Reflection;
using System.Windows;

namespace Ags.MonsterTools
{
    public class Bootstrapper : UnityBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            this.Container.RegisterType<MainWindow>(new ContainerControlledLifetimeManager());
            return this.Container.Resolve<MainWindow>();
        }

        protected override IUnityContainer CreateContainer()
        {
            return new UnityContainer();
        }

        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();
            this.Container.RegisterType<IStatusbarService, StatusbarServiceImpl>(
                new ContainerControlledLifetimeManager());
            this.Container.RegisterType<IMainWindowHandler, MainWindowHandler>(new ContainerControlledLifetimeManager());
            this.Container.RegisterType<IViewManager, ViewManagerImpl>(new ContainerControlledLifetimeManager());
            this.Container.RegisterInstance(typeof(ILogger), this.Logger);
            this.Container.RegisterType(typeof(AboutWindow), new TransientLifetimeManager());
        }

        protected override void InitializeShell()
        {
            base.InitializeShell();
            Application.Current.MainWindow = (Window)this.Shell;
            Application.Current.MainWindow.Show();
        }

        protected override IModuleCatalog CreateModuleCatalog()
        {
            var catalog = new DirectoryModuleCatalog
            {
                ModulePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
            };
            return catalog;
        }

        protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
        {
            RegionAdapterMappings mappings = base.ConfigureRegionAdapterMappings();
            mappings.RegisterMapping(typeof(Window), Container.Resolve<WindowRegionAdapter>());
            return mappings;
        }

        protected override ILoggerFacade CreateLogger()
        {
            return new LoggerImpl();
        }
    }
}
